package com.speciality.demo.demos.service.impl;

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

@Service
public class WeatherDataConnService {
    @Autowired
    private SparkSession sparkSession;

    public Dataset<Row> loadHistoricalData(LocalDate startDate, LocalDate endDate) {
        return sparkSession.read()
                .format("jdbc")
                .option("url", "jdbc:mysql://localhost:3306/meteorology") // 替换数据库信息
                .option("dbtable", String.format(
                        "(SELECT * FROM weather_hourly WHERE time BETWEEN '%s 00:00:00' AND '%s 23:59:59') AS sub",
                        startDate, endDate
                ))
                .option("user", "root")
                .option("password", "root")
                .option("driver", "com.mysql.cj.jdbc.Driver")
                .load();
    }
}
